package com.asiainfo.demo.control;

import com.asiainfo.demo.dao.roles.Card;
import com.asiainfo.demo.dao.roles.Member;
import com.asiainfo.demo.service.CardManage;
import com.asiainfo.demo.service.MemberManage;

public class ui {
    public static void main(String[] argv){
        if(argv.length < 2) {
            System.out.println("WRONG ARGV");
            System.exit(-1);
        }
        switch (argv[0]){
            case "add-member": {
                Member member = new Member(argv[1], argv[2], argv[3], (byte) 1);
                MemberManage.addMember(member);
                System.out.println("OK. " + argv[1] + "'s ID is " +
                        MemberManage.findMemberByName(argv[1]).getMemID() + ".");
                break;
            }
            case "add-card": {
                Member member = MemberManage.findMemberByName(argv[1]);
                byte cardType = argv[2].equals("年卡") ? (byte) 1 : (byte) 0;
                if(member == null){
                    System.out.println("Member Not Found.");
                }
                else {
                    Card card = new Card(cardType, (byte) 1);

                    try {
                        int maxID = CardManage.findMaxCardID();
                        if(maxID == -1){
                            card.setCardID(1);
                        }
                        else{
                            card.setCardID(maxID + 1);
                        }

                        int times = argv.length == 4 ? Integer.valueOf(argv[3]) : -1;
                        MemberManage.bindCard(member, card, times);
                        System.out.println("A new year card added to " + argv[1]+".");
                        System.out.println("A new" + argv[1]+"with 100 times added to"+argv[1]+".");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                break;
            }
            //UPDATE: other case
            //case "add course"
            default:
                System.out.println("WRONG FUNCTION");
        }
    }
}
